package sel.me.spring;

import java.util.Date;

import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MissingRequiredPropertiesException;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import sel.me.spring.beans.TestBeanA;


public class ApplicationContextTest {

	public static void main(String[] args) throws Exception {
        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();

        Resource[] resources = resourcePatternResolver.getResources("classpath*:spring/spring-*.xml");
        
        for (Resource resource : resources) {
            System.out.println(resource.getFilename());
            System.out.println(resource.getURL());
            System.out.println(resource.getURI());
            System.out.println("=================");
        }
        
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("classpath*:spring/spring-*.xml");
        
        for(String beanName : classPathXmlApplicationContext.getBeanDefinitionNames()){
        	System.out.println(beanName);
        }
        
        System.out.println(classPathXmlApplicationContext.containsBean("testBeanA"));
        
        System.out.println(classPathXmlApplicationContext.getBeanFactoryPostProcessors());
        
        System.out.println(classPathXmlApplicationContext.getApplicationListeners());
        
        System.out.println(classPathXmlApplicationContext.getBeanFactory());
        
        System.out.println(classPathXmlApplicationContext.getEnvironment());
        
        
        System.out.println(classPathXmlApplicationContext.getBean("factoryBeanOne"));
        System.out.println(classPathXmlApplicationContext.getBean("factoryBeanOne").hashCode());
//        Thread.sleep(5000);
        System.out.println("factoryBeanOne.hashCode()="+classPathXmlApplicationContext.getBean("factoryBeanOne").hashCode());
        System.out.println(classPathXmlApplicationContext.getBean("factoryBeanTwo"));
        System.out.println("factoryBeanOne.getClass()="+classPathXmlApplicationContext.getBean("factoryBeanOne").getClass());
        System.out.println("factoryBeanTwo.getClass()="+classPathXmlApplicationContext.getBean("factoryBeanTwo").getClass());
        
        TestBeanA testBeanA = (TestBeanA)classPathXmlApplicationContext.getBean("testBeanA");
        testBeanA.testFactoryBean();
        testBeanA.testObjectFactory();
        
        //Date类重写了生成hash码的方法，以系统时间为参照生成当前对象的hash码，因此，如果在同一时间做new Date()操作，那么在这个运算下生成的hashCode就是相等
        System.out.println(new Date().hashCode());
        System.out.println(new Date().hashCode());
        
        
        classPathXmlApplicationContext.close();
    }
}
